<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('资格证书列表')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">
    <div class="ui-layout-west" shiro:hasPermission="system:certificate:deptTree">
        <div class="box box-main">
            <div class="box-header">
                <div class="box-title">
                    <i class="fa fa-sitemap"></i> 组织机构
                </div>
                <div class="box-tools pull-right">
                    <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i class="fa fa-chevron-up"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i class="fa fa-chevron-down"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新部门"><i class="fa fa-refresh"></i></button>
                </div>
            </div>
            <div class="ui-layout-content">
                <div id="tree" class="ztree"></div>
            </div>
        </div>
    </div>

     <div class="ui-layout-center">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" id="deptId" name="deptId">
                    <input type="hidden" id="parentId" name="parentId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>证书名：</label>
                                <input type="text" name="cerName"/>
                            </li>
                            <li>
                                <label>发布机构：</label>
                                <input type="text" name="cerInstitution"/>
                            </li>
                            <li>
                                <label>证书获取时间：</label>
                                <input type="text" class="time-input" placeholder="请选择证书获取时间" name="cerGettime"/>
                            </li>
                            <li>
                                <label>过期情况：</label>
                                <select type="text" name="isExpired">
                                    <option value="">所有</option>
                                    <option value="0">过期</option>
                                    <option value="1">临近过期</option>
                                    <option value="2">未过期</option>
                                    <option value="3">状态未知</option>
                                </select>
                            </li>
                            <li>
                                <label>指导老师：</label>
                                <input type="text" name="cerInstructor"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:certificate:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:certificate:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:certificate:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:certificate:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-warning" onclick="downloadFiles()" shiro:hasPermission="system:certificate:download">
                    <i class="fa fa-download"></i> 导出文件
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: layout-latest-js" />
    <th:block th:include="include :: ztree-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:certificate:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:certificate:remove')}]];
        var downloadFlag = [[${@permission.hasPermi('system:certificate:download')}]];
        var pdfViewFlag = [[${@permission.hasPermi('system:certificate:pdfView')}]];
        var prefix = ctx + "system/certificate";

		$(function() {
		    var panehHidden = false;
		    if ($(this).width() < 769) {
		        panehHidden = true;
		    }
		    $('body').layout({ initClosed: panehHidden, west__size: 185, resizeWithWindow: false });
	     	// 回到顶部绑定
	    	if ($.fn.toTop !== undefined) {
	    		var opt = {
	    			win:$('.ui-layout-center'),
	    			doc:$('.ui-layout-center')
	    		};
	    		$('#scroll-up').toTop(opt);
	    	}
		    queryUserList();
		    queryDeptTree();
		});

        function queryUserList() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                downloadFilesUrl:prefix + "/download/resources",
                clickToSelect: true,
                maintainSelected: true,
                modalName: "资格证书",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'cerId',
                    title: '证书ID'
                },
                {
                    field: 'cerName',
                    title: '证书名'
                },
                /*,
                {
                    field: 'stuClass',
                    title: ' 所属班级',
                    visible: false
                },
                {
                    field: 'stuSpeciality',
                    title: ' 所属专业',
                    visible: false
                },*/
                {
                    field: 'cerInstitution',
                    title: '发布机构'
                },
                {
                    field: 'cerGettime',
                    title: '证书获取时间'
                },
                {
                    field: 'cerDeadline',
                    title: '证书过期时间'
                },
                {
                    field: 'isExpired',
                    title: '是否过期',
                    formatter: function(value, item, index) {
		                if (item.isExpired == '0') {
		                    return '<span class="badge badge-danger">已过期</span>';
		                }
		                else if (item.isExpired == '1') {
		                    return '<span class="badge badge-warning">临近过期</span>';
		                }
		                else if (item.isExpired == '2') {
		                    return '<span class="badge badge-info">未过期</span>';
		                }
		                else if (item.isExpired == '3') {
		                    return '<span class="badge badge-success">状态未知</span>';
		                }
		            }
                },
                {
                    field: 'cerInstructor',
                    title: '指导老师'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    field: 'userId',
                    title: '所属账号Id'
                },
                {
                    field: 'filePath',
                    title: '附件',
                    visible: false
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.cerId + '\')" style="margin:0px 2px;"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.cerId + '\')" style="margin:0px 2px;"><i class="fa fa-remove"></i>删除</a>');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="removeFile(' + row.cerId + ')"><i class="fa fa-window-close-o" style="margin:0px 2px;"></i>删除文件</a>');
                        actions.push('<a class="btn btn-primary btn-xs ' + downloadFlag + '" href="javascript:void(0)" onclick="downloadFile(\'' + row.cerId + '\'\,\'' + row.filePath + '\')" style="margin:0px 2px;"><i class="fa fa-cloud-download"></i>下载文件</a>');
                        actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="pdfView(\'' + row.cerId + '\')" style="margin:0px 2px;"><i class="fa fa-cloud-download"></i>证书预览</a>');
			            //actions.push("<a class='btn btn-default btn-xs " + editFlag + "' href='javascript:void(0)' onclick='authStudentData(" + row.cerId + ")'><i class='fa fa-check-square-o'></i>分配学生</a>");
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        }

        /* 在列表中删除资格证书（文件和修改数据库）*/
        function removeFile(cerId) {
            $.modal.confirm("确认要删除选中的证书Id："+ cerId + "的数据吗?", function() {
                var data = { "cerId": cerId };
                $.ajax({
                    url: prefix + "/remove/"+ cerId,
                    type: 'post',
                    cache: false,
                    data: {filePath:''},
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
            });
        }

        function editFile() {
            var formData = new FormData();
            if ($('#uploadFilePath')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            formData.append('file', $('#uploadFilePath')[0].files[0]);
            $.ajax({
                url: prefix + "/editFile",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }

        function downloadFile(cerId,value){
            if(value == null || value == ""){
                $.modal.alert("文件未找到或者文件不存在！");
                return false;
            }else{
                window.location.href = prefix + "/download/resource?cerId=" + cerId;
            }
        }

        function downloadFiles() {
            table.set();
            var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.modal.confirm("确认要下载选中的"+ rows.length + "条数据吗?", function() {
                var url = table.options.downloadFiles;
                window.location.href = prefix + "/download/resources?cerIds=" + rows;
            });
        }

		/* 用户管理-分配角色 */
		function authStudentData(cerId) {
		    var url = prefix + '/authStudentData/' + cerId;
		    $.modal.openTab("证书分配学生信息", url);
		}

		/* 用户管理-分配角色 */
		function pdfView(cerId) {
		    var url = prefix + '/pdfView/' + cerId;
		    $.modal.openTab("证书预览", url);
		}

		function queryDeptTree()
		{
			var url = ctx + "system/certificate/deptTreeData";
			var options = {
		        url: url,
		        expandLevel: 2,
		        onClick : zOnClick
		    };
			$.tree.init(options);

			function zOnClick(event, treeId, treeNode) {
				$("#deptId").val(treeNode.id);
				$("#parentId").val(treeNode.pId);
				$.table.search();
			}
		}

		$('#btnExpand').click(function() {
			$._tree.expandAll(true);
		    $(this).hide();
		    $('#btnCollapse').show();
		});

		$('#btnCollapse').click(function() {
			$._tree.expandAll(false);
		    $(this).hide();
		    $('#btnExpand').show();
		});

		$('#btnRefresh').click(function() {
			queryDeptTree();
		});
    </script>
</body>
</html>